package org.inspursc.s0517.health.evaluation.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.inspursc.s0517.health.evaluation.converter.ReportShareConverter;
import org.inspursc.s0517.health.evaluation.dao.ReportShareDao;
import org.inspursc.s0517.health.evaluation.dto.ReportShareDTO;
import org.inspursc.s0517.health.evaluation.entity.ReportShare;
import org.inspursc.s0517.health.evaluation.service.ReportShareService;
import org.inspursc.s0517.health.evaluation.utils.DateUtil;
import org.inspursc.s0517.health.evaluation.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author yuchu
 */
@Service
@Slf4j
public class ReportShareServiceImpl implements ReportShareService {

    @Autowired
    private ReportShareDao reportShareDao;

    @Override
    public ReportShareDTO saveReportShare(ReportShareDTO dto) {
        String pass=StringUtil.generateWord();
        Date date=DateUtil.getDateAfter(new Date(),7);
        dto.setPass(pass);
        dto.setDate(date);
        ReportShare reportShare=ReportShareConverter.converter(dto);
        reportShareDao.save(reportShare);
        dto.setId(reportShare.getId());
        return dto;
    }

    @Override
    public ReportShareDTO findByReportIdAndPassAndDateGreaterThanEqual(int reportId, String pass, Date date) {
        ReportShare reportShare=reportShareDao.findByReportIdAndPassAndDateGreaterThanEqual(reportId,pass,date);
        if(null!=reportShare){
            return ReportShareConverter.converter(reportShare);
        }
        return null;
    }

    @Override
    public ReportShareDTO findById(int id) {
        return ReportShareConverter.converter(reportShareDao.getOne(id));
    }


    @Override
    public ReportShareDTO findByReportIdAndDateGreaterThanEqual(int reportId, Date date) {
        ReportShare reportShare=reportShareDao.findByReportIdAndDateGreaterThanEqual(reportId,date);
        if(null!=reportShare){
            return ReportShareConverter.converter(reportShare);
        }
        return null;
    }

    @Override
    public void deleteById(int id) {
        reportShareDao.deleteById(id);
    }
}
